package com.trg.flex.frm
{
	import com.trg.flex.frm.validation.InputValidationHelper;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.controls.RadioButtonGroup;
	import mx.core.IFlexDisplayObject;
	import mx.events.FlexEvent;

	[Event(name="validChanged", type="mx.events.PropertyChangeEvent")]
	public class FormRadioButtonGroup extends RadioButtonGroup implements FormInput
	{
		public function FormRadioButtonGroup(document:IFlexDisplayObject=null)
		{
			super(document);
			addEventListener("numRadioButtonsChanged", numRadioButtonsChangedHandler);
		}
		
		protected var _property:String;
		
		public function get elementValue():Object
		{
			return selectedValue;
		}
		
		public function set elementValue(value:Object):void
		{
			if (value != null) {
				selectedValue = value;
				if (selectedValue == value) return;
			}

			//select none
			if (selection != null) selection.selected = false;
			validationHelper.setNotValidated();
		}
		
		public function get property():String
		{
			return _property;
		}
		
		public function set property(property:String):void
		{
			_property = property;
		}

	//Find FormContainer
		protected var formContainer:FormContainer;
		protected var topContainer:DisplayObject;
		
		protected function addListener(event:FlexEvent):void {
			topContainer.removeEventListener(FlexEvent.ADD, addListener);
			
			while(true) {
				if (topContainer.parent is FormContainer) {
					formContainer = topContainer.parent as FormContainer;
					formContainer.addElement(this);
					break;
				} else if (topContainer.parent == null) {
					topContainer.addEventListener(FlexEvent.ADD, addListener);
					break;
				}
				topContainer = topContainer.parent;
			}
		}
		
		/**
		 * Listen for the first radio button to be added.
		 * Use that button to find the form container.
		 */
		protected function numRadioButtonsChangedHandler(event:Event):void {
			if (numRadioButtons > 0) {
				topContainer = getRadioButtonAt(0);
				getRadioButtonAt(0).addEventListener(FlexEvent.ADD, addListener);
				removeEventListener("numRadioButtonsChanged", numRadioButtonsChangedHandler);
				getRadioButtonAt(0).dispatchEvent(new FlexEvent(FlexEvent.ADD));
			}
		}
				
	//Validation
		public function set errorString(value:String):void {
			for (var i:int = 0; i < numRadioButtons; i++) {
				getRadioButtonAt(i).errorString = value;
			}
		}
		
		protected var validationHelper:InputValidationHelper = new InputValidationHelper(this as FormInput, "selectedValue", FlexEvent.VALUE_COMMIT);
		
		[Bindable("validChanged")]
		public function get valid():Boolean {
			return validationHelper.valid;
		}
		
		public function isInvalidRequired():Boolean {
			return validationHelper.isInvalidRequired();
		}
		
		public function clearValidation():void {
			errorString = "";
		}
		
		public function set type(value:Class):void {
			validationHelper.type = value;
		}
		
		public function setMetaData(metaData:Object):void {
			validationHelper.setMetaData(metaData);
		}
		
		public function set notNull(value:Boolean):void {
			validationHelper.setMetaDataProp("notNull", value ? true : undefined);
		}
		
		public function set notEmpty(value:Boolean):void {
			validationHelper.setMetaDataProp("notEmpty", value ? true : undefined);
		}
		
		public function set maxValue(value:Object):void {
			validationHelper.setMetaDataProp("maxValue", value != null ? value : undefined);
		}
		
		public function set minValue(value:Object):void {
			validationHelper.setMetaDataProp("minValue", value != null ? value : undefined);
		}
	}
}